/*
 * thread.h
 *
 *  Created on: 03/10/2011
 *      Author: guido
 */

#ifndef THREAD_H_
#define THREAD_H_

#include <pthread.h>
#include "runnable.h"

class Thread {
public:
	Thread() {
		isAlive = true;
	}
	void start() {
		pthread_create(&thread, NULL, Thread::startThread, (void*) this);
	}
	~Thread() {
	}
	virtual void run() = 0;
	bool isAlive() {
		return isAlive;
	}
	void signalDead() {
		isAlive = false;
	}
	void esperarFin() {
		pthread_join(thread, NULL);
	}
private:
	static void* startThread(void* arg) {
		Thread* thread = static_cast<Thread*>(arg);
		thread->run();
		thread->signalDead();
		return NULL;
	}
	pthread_t thread;
	bool isAlive;
};

#endif /* THREAD_H_ */
